<template>
  <div class='aui-card--fill scroll-box card-box'>
    <div class='pane-shell'>
      <div class="header">
        <!-- v-if="$hasPermission('system:sysCompany:updateCompany')" -->
        <el-button type="primary"
                   @click="dataFormSubmitHandle">修改</el-button>
      </div>
      <el-form :model="dataForm"
               v-loading="dataListLoading"
               ref="dataForm"
               label-position="left"
               @keyup.enter.native="dataFormSubmitHandle()"
               label-width="130px">
        <el-form-item prop="androidVersion"
                      :rules="stringRule()"
                      label="安卓版本号">
          <el-input v-model.trim="dataForm.androidVersion"></el-input>
        </el-form-item>
        <el-form-item prop="processVersion"
                      label="安卓审核版本号">
          <el-input v-model.trim="dataForm.processVersion"></el-input>
        </el-form-item>
        <!-- <el-form-item prop="iosVersion"
                      :rules="stringRule()"
                      label="苹果版本号">
          <el-input v-model.trim="dataForm.iosVersion"></el-input>
        </el-form-item> -->
        <!-- <el-form-item prop="addAppVersionNo"
                        label="苹果审核版本号">
            <el-input v-model.trim="dataForm.addAppVersionNo"></el-input>
          </el-form-item>
        <el-form-item prop="iosAppId"
                      :rules="stringRule(false,1,50)"
                      label="苹果的appid">
          <el-input v-model.trim="dataForm.iosAppId"></el-input>
        </el-form-item> -->
        <el-form-item label="android版本链接">
          <file-upload ref="fileUpload"
                       reg=".(apk)$"
                       accept=".apk"
                       :limit="1"
                       :isLt2M="50"
                       :paramData="{ bucketName: 'appUrl/apk/' }"
                       action="/springbootApi/system/oss/upload"
                       @success="successLoad"
                       @remove="handleRemove" />
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import debounce from 'lodash/debounce'
import fileUpload from '@/components/file-upload/file-upload'
export default {
  mixins: [mixinViewModule, minFormValidate],
  name: '',
  data () {
    return {
      mixinViewModuleOptions: {
        activatedIsNeed: false,
        deleteImgURL: '/system/oss/delete',
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        getDataInfoURL: '', // 数据详情，API地址
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      rests: [],
      dataForm: {
        androidVersion: '',
        // iosVersion: '',
        // iosAppId: '',
        processVersion: '',
        versionAddress: ''
      }
    }
  },
  components: {
    fileUpload
  },
  computed: {
  },
  mounted () {
    this.rests = []
    this.getInfo()
  },
  methods: {
    getInfo () {
      this.getData({
        url: '/tb/versionControlController/getVersion'
      }).then((res) => {
        if (res.code !== 0) {
          return
        }

        this.dataForm = res.data ? res.data : this.dataForm
        this.rests.push({
          name: res.data.versionAddress.substring(res.data.versionAddress.lastIndexOf('-') + 1),
          url: res.data.versionAddress
        })
        this.$refs.fileUpload.init(this.rests)
      })
    },
    // 文件上传成功
    successLoad (name, url) {
      this.rests.push({
        name,
        url
      })
    },
    // 监听是否删除文件
    handleRemove (file, index) {
      // 如果删除的文件为编辑时新添加的文件的话立即调用删除接口
      this.deleteFile(file.url)
      this.rests.splice(index, 1)
    },
    // 删除文件
    deleteFile (path) {
      this.getDataList({ url: this.mixinViewModuleOptions.deleteURL, paramData: { fileName: path } })
        .then(res => {
          if (res.code !== 0) {
            return false
          }
          this.rests = []
        })
    },
    // 删除图片
    deleteImg (url) {
      this.getDataList({ url: this.mixinViewModuleOptions.deleteImgURL, paramData: { url }, isLoading: false })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (!this.rests[0]) {
            return this.$message.warning('请上传安装包')
          }
          this.dataForm.versionAddress = this.rests[0].url
          delete this.dataForm.id
          this.putData({ url: '/tb/versionControlController/updateVersion' }).then(res => {
            if (res.code !== 0) {
              return false
            }
            this.rests = []
            this.dataForm = {}
            this.getInfo()
            this.$message.success('修改成功')
          })
        }
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss" scoped>
.header {
  width: 100%;
  text-align: right;
  margin-bottom: 20px;
}
</style>
